import { physics } from 'cc';
import { _decorator, Component, Node, director, Camera, geometry, PhysicsSystem, game, Vec3, math, v3, tween } from 'cc';
import { EventDispatcher } from '../../ngame/util/EventDispatcher';
import GameConst from '../GameConst';
import { IClick } from '../scene/IClick';
import { ICameraController } from './ICameraController';
import { IRoleController } from './IRoleController';
import { ISoldierController } from './role/ISoldierController';
const { ccclass, property } = _decorator;

/**
 * 玩家控制器
 */
@ccclass('IPlayerController')
export class IPlayerController extends IRoleController {

    camera:Camera = null;

    onLoad(){

        super.onLoad();

    }

    initData(data){

        console.log("IPlayerController - initData",data,GameConst.USER_ID);

        if(data && data.userId === GameConst.USER_ID){
            this.onEvent();
            
            //绑定相机
            let iCameraController = director.getScene().getComponentInChildren(ICameraController);
            iCameraController.target = this.node;
            this.camera = iCameraController.getComponentInChildren(Camera);
            this.maxHistoryMoveNumber = 20;
        }

    }
    

    onDestroy(){

        this.offEvent();

    }

    onEvent(){

        console.log("绑定事件");
        //绑定点击地图事件
        EventDispatcher.getIns().on(GameConst.UI_TOUCH.CLICK_RECRUIT,this.clickRecruit,this);
        EventDispatcher.getIns().on(GameConst.UI_TOUCH.CLICK_ATTACK,this.clickAttack,this);
        EventDispatcher.getIns().on(GameConst.TOUCH.TOUCH_WORLD_CLICK_FLOOR,this.clickWorldFloor,this);
        EventDispatcher.getIns().on(GameConst.TOUCH.TOUCH_WORLD_CLICK_OBJECT,this.clickWorldObject,this);

    }
    offEvent(){

        //绑定点击地图事件
        EventDispatcher.getIns().off(GameConst.UI_TOUCH.CLICK_RECRUIT,this.clickRecruit);
        EventDispatcher.getIns().off(GameConst.UI_TOUCH.CLICK_ATTACK,this.clickAttack,this);
        EventDispatcher.getIns().off(GameConst.TOUCH.TOUCH_WORLD_CLICK_FLOOR,this.clickWorldFloor,this);
        EventDispatcher.getIns().off(GameConst.TOUCH.TOUCH_WORLD_CLICK_OBJECT,this.clickWorldObject,this);
        
    }

    AnimFun_玩家攻击(){

        console.log("AnimFun_玩家攻击");

    }

    //点击地图
    clickWorldFloor(raycastClosestResult:physics.PhysicsRayResult){
        this.rServerEvent("moveWorldPlayer",raycastClosestResult.hitPoint.clone());
    }

    //点击物体
    clickWorldObject(click:IClick){
        click.event(this);
    }

    //点击招募士兵
    clickRecruit(){
        director.getScene().getComponentsInChildren(ISoldierController).forEach(soldider => soldider.setFollow(this))
    }

    //攻击
    clickAttack(){
        this.playAnim("玩家攻击");
    }
    
}

